package com.example.smartcity.ui.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.smartcity.R
import com.example.smartcity.SmartCityApplication.Companion.context
import com.example.smartcity.ui.home.model.NewsEntity

/**
 *  ANDROID STUDIO @version 2022.1
 *          @email 2506789532@qq.com  @ikun
 *  time: 2023/1/22 23:20
 **/
class NewsRecyclerViewAdapter(
    private val listData: List<NewsEntity>
) : RecyclerView.Adapter<NewsRecyclerViewAdapter.ViewHolder>(){
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        // 获取控件
        val imageView = view.findViewById<ImageView>(R.id.newsImageView)
        val content = view.findViewById<TextView>(R.id.newsContent)
        val time = view.findViewById<TextView>(R.id.newsTime)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val root = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item_news, parent, false)
        val view = ViewHolder(root)

        view.itemView.setOnClickListener {
            Toast.makeText(context, "你不要点击我啊！", Toast.LENGTH_SHORT).show()
        }

        return view
    }

    override fun getItemCount() = listData.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = listData[position]
        Glide.with(context)
            .load(data.imageUrl)
            .into(holder.imageView)
        holder.content.text = data.content
        holder.time.text = data.newsTime
    }
}